#!/usr/bin/env Rscript

suppressPackageStartupMessages(library("argparse"))

parser = ArgumentParser(description="See GOplot documentation: https://wencke.github.io/")
parser$add_argument("--EC_david", help="EC.david input file", required=TRUE)
parser$add_argument("--EC_genelist", help="EC.genelist input file", required=TRUE)
parser$add_argument("--pdf_outfile", help="pdf output filename", required=TRUE)

args = parser$parse_args()

library(GOplot)
EC.david = read.table(args$EC_david, sep='\t', header=T, quote="")
EC.genelist = read.table(args$EC_genelist, sep='\t', header=T)

# plotting commands from: https://wencke.github.io/
circ <- circle_dat(EC.david, EC.genelist)

message("Generating plot pdf file: ", args$pdf_outfile)
pdf(file=args$pdf_outfile)

###############################
## The modified barplot (GOBar)
###############################

## Generate a simple barplot
message("GOBar, circ, BP")
GOBar(subset(circ, category == 'BP'))

## Facet the barplot according to the categories of the terms
message("GOBar, circ, multiple")
GOBar(circ, display = 'multiple')

# Facet the barplot, add a title and change the colour scale for the z-score
message("GOBar, multiple, Z-scores")
GOBar(circ, display = 'multiple', title = 'Z-score coloured barplot', zsc.col = c('yellow', 'black', 'cyan'))


#############################
## The bubble plot (GOBubble)
#############################


# Generate the bubble plot with a label threshold of 3
#message("GOBubble, circ")
#GOBubble(circ, labels = 3)

# Add a title, change the colour of the circles, facet the plot according to the categories and change the label threshold
message("GOBubble, multiple")
GOBubble(circ, title = 'Bubble plot', col = c('orange', 'darkred', 'gold'), display = 'multiple', labels = 3)

## Colour the background according to the category
message("GOBubble, bckgrd color")
GOBubble(circ, title = 'Bubble plot with background colour', display = 'multiple', labels = 3)

# Reduce redundant terms with a gene overlap >= 0.75...
#reduced_circ <- reduce_overlap(circ, overlap = 0.75)
## ...and plot it
#message("GOBubble, reduced")
#GOBubble(reduced_circ, labels = 2.8)



###########################################################################################
## Circular visualization of the results of gene- annotation enrichment analysis (GOCircle)
###########################################################################################


#Generate a circular visualization of the results of gene- annotation enrichment analysis
message("GOCircle")
GOCircle(circ)

# Generate a circular visualization of selected terms
# (set the ids to your liking)
#IDs <- c('GO:0007507', 'GO:0001568', 'GO:0001944', 'GO:0048729', 'GO:0048514', 'GO:0005886', 'GO:0008092', 'GO:0008047')
#GOCircle(circ, nsub = IDs)

# Generate a circular visualization for 10 terms
message("GOcircule, nsub=10")
GOCircle(circ, nsub = 10)


##### Now, the rest is up to you. ;-)

quit(save = "yes", status = 0, runLast = FALSE)

